﻿using System;
abstract class Figure
{
    public abstract double area();
    public abstract bool judge();    
}
class Triangular : Figure
{
    int a { get; set; }
    int b { get; set; }
    int c { get; set; }
    public Triangular(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public override double area()
    {
        if (judge())
        {
            double p = (a + b + c) / 2;
            return System.Math.Sqrt(p * (p - a) * (p - b) * (p - c));
        }
        else return 0;
    }
    public override bool judge()
    {
        if (a + b > c && a + c > b && b + c > a)
        {
            return true;
        }
        else return false;
    }
}
class Rectangular : Figure
{
    int a { get; set; }
    int b { get; set; }
    public Rectangular(int a, int b)
    {
        this.a = a;
        this.b = b;
    }
    public override double area()
    {
        if (judge())
        {
            return a * b;
        }
        else return 0;
    }
    public override bool judge()
    {
        if (a > 0 && b> 0)
        {
            return true;
        }
        else return false;
    }
}
class Square : Figure
{
    int a { get; set; }
    public Square(int a)
    {
        this.a = a;
    }
    public override double area()
    {
        if (judge())
        {
            return a * a;
        }
        else return 0;
    }
    public override bool judge()
    {
        if (a > 0)
        {
            return true;
        }
        else return false;
    }
}
class Factory
{
    public static Figure GetProduct(string arg)
    {
        string[] s = arg.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 3)
        {
            int a = int.Parse(s[0]);
            int b = int.Parse(s[1]);
            int c = int.Parse(s[2]);
            Console.WriteLine("创建了一个边长为{0}, {1}, {2}的三角形", a, b, c);
            return new Triangular(a, b, c);
        }
        else if (s.Length == 2)
        {
            int a = int.Parse(s[0]);
            int b = int.Parse(s[1]);
            Console.WriteLine("创建了一个边长为{0}, {1}的长方形", a, b);
            return new Rectangular(a, b);
        }
        else if (s.Length == 1)
        {
            int a = int.Parse(s[0]);
            Console.WriteLine("创建了一个边长为{0}的正方形", a);
            return new Square(a);
        }
        else
        {
            return null;
        }
    }
}
class Program
{
    public static void Main()
    {
        Figure f = null;
        f = Factory.GetProduct("3, 4, 5");
        if (f.judge())
        {
            Console.WriteLine("该形状合法");
            Console.WriteLine("该形状的面积为{0}", f.area());
        }
        else
        {
            Console.WriteLine("该形状不合法");
        }
        Console.WriteLine();

        f = Factory.GetProduct("1, 2");
        if (f.judge())
        {
            Console.WriteLine("该形状合法");
            Console.WriteLine("该形状的面积为{0}", f.area());
        }
        else
        {
            Console.WriteLine("该形状不合法");
        }
        Console.WriteLine();

        f = Factory.GetProduct("0");
        if (f.judge())
        {
            Console.WriteLine("该形状合法");
            Console.WriteLine("该形状的面积为{0}", f.area());
        }
        else
        {
            Console.WriteLine("该形状不合法");
        }
    }
}
